package org.misalen.web.service.sms.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;

import org.misalen.web.service.sms.CcpSms;
import org.misalen.web.service.sms.info.CcpInfoType;
import org.springframework.stereotype.Service;

import com.cloopen.rest.sdk.CCPRestSmsSDK;
@Service
public class CCPSmsImpl implements CcpSms {
	CCPRestSmsSDK restAPI = new CCPRestSmsSDK();
	static Properties p = new Properties();
	static {
		InputStream in = CCPSmsImpl.class.getClassLoader().getResourceAsStream("pro.properties");
		try {
			p.load(in);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public CCPSmsImpl() {
		restAPI.init(p.getProperty("sms.ccp.baseUrl"), p.getProperty("sms.ccp.serverPort"));
		restAPI.setAccount(p.getProperty("sms.ccp.accountSid"), p.getProperty("sms.ccp.accountToken"));
		restAPI.setAppId(p.getProperty("sms.ccp.AppId"));
	}

	@Override
	public boolean send(String mobile, CcpInfoType infoType, String... strings) {
		HashMap<String, Object> resultSms = restAPI.sendTemplateSMS(mobile, p.getProperty(infoType.templateId),
				strings);
		String code = resultSms.get("statusCode").toString();
		return "000000".equals(code);
	}
}
